﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.ComponentModel;
using xTeam.HengLe.DataAccess;
using xTeam.HengLe.Entity;
using Webdiyer.WebControls.Mvc;

namespace xTeam.HengLe.Controller.Website
{
    public class LeaveWordController : BaseController
    {
        public LeaveWordController()
        {
            // 数据连接字符串
            LeaveWordReplyDataAccess.Con = _connectionString;
        }

        public ActionResult Index([DefaultValue(1)]int pageid)
        {
            int total = 0;
            List<ELeaveWord> leaveWordList = LeaveWordReplyDataAccess.GetLeaveWordReply(10, pageid, 0, ref total);
            ViewData["Pager"] = this.CreatePager(Url.Action("Index", new { pageid = "{0}" }), total, pageid);
            return View(leaveWordList);
        }
        [HttpPost]
        public ActionResult Index([DefaultValue("")]string name, [DefaultValue("")]string contactWay, [DefaultValue("")]string content, [DefaultValue(1)]int pageid)
        {
            string msg = string.Empty;

            if (string.IsNullOrEmpty(name))
            {
                msg = "你的姓名不能为空！";
            }
            else if (name.Length > 10)
            {
                msg = "留言内容不能超过10个字！";
            }
            else if (string.IsNullOrEmpty(contactWay))
            {
                msg = "联系方式不能为空！";
            }
            else if (contactWay.Length > 100)
            {
                msg = "留言内容不能超过100个字！";
            }
            else if (string.IsNullOrEmpty(content))
            {
                msg = "留言内容不能为空！";
            }
            else if (content.Length > 500)
            {
                msg = "留言内容不能超过500个字！";
            }
            else
            {
                int ret = LeaveWordReplyDataAccess.EditLeaveWord(new ELeaveWord { Name = name, ContactWay = contactWay, Content = content });

                //if (ret > 0)
                //{
                msg = "留言发表成功！";
                return Content(string.Format("alert('{0}');location.href='" + Url.Action("Index", "LeaveWord", new { pageid = pageid }) + "';", msg));
                //}
                //else
                //{
                //    msg = "留言发表失败！";
                //}
            }

            return Content(string.Format("alert('{0}');", msg));
        }
    }
}
